<template>
    <div class="container auth-group-container">
        <div class="operation-wrapper">
            <el-button
                type="primary"
                size="mini"
                :disabled="!authResult.auth_edit"
                @click="submitGroup">保存</el-button>
        </div>
        <el-row class="tree-row">
            <el-col
                class="tree-col"
                :md="12"
                :lg="10"
                :xl="8">
                <el-tree
                    :data="groupList"
                    :props="{label: 'title'}"
                    empty-text="暂无分组">
                    <div
                        class="custom-tree-node"
                        slot-scope="{ node, data }">
                        <div class="custom-tree-node__content">
                            <template v-if="data.isEditingTitle">
                                <el-input
                                    :ref="`edit_${data.group_no || data.new_id}`"
                                    v-model="data.tempTopTitle"
                                    size="mini"
                                    @click.native.stop
                                    @keyup.enter.native="handleResolveEditTitle(data)"/>
                                <span style="margin-left: 20px;">
                                    <el-button
                                        type="text"
                                        size="mini"
                                        @click.stop="handleResolveEditTitle(data)">
                                        保存
                                    </el-button>
                                    <el-button
                                        type="text"
                                        size="mini"
                                        @click.stop="handleHideEditTitle(data)">
                                        取消
                                    </el-button>
                                </span>
                            </template>
                            <template v-else>
                                <span>{{ node.label }}</span>
                                <span>
                                    <el-button
                                        type="text"
                                        size="mini"
                                        @click.stop="handleShowAddSubLevel(data)">
                                        添加子组
                                    </el-button>
                                    <el-button
                                        type="text"
                                        size="mini"
                                        @click.stop="handleShowEditTitle(data)">
                                        重命名
                                    </el-button>
                                    <el-button
                                        class="danger"
                                        type="text"
                                        size="mini"
                                        @click.stop="handleDeletSubLevel(node,data)">
                                        删除分组
                                    </el-button>
                                </span>
                            </template>
                        </div>
                        <div
                            class="add-tree-node"
                            v-if="data.isAddingSubLevelGroup"
                            @click.stop>
                            <el-input
                                :ref="`new_sub_${data.group_no || data.new_id}`"
                                v-model="data.tempTitle"
                                size="mini"
                                @keyup.enter.native="handleResolveAddSubLevelGroup(data)" />
                            <div class="panel-header-operation">
                                <el-button
                                    type="text"
                                    @click="handleResolveAddSubLevelGroup(data)">确定</el-button>
                                <el-button
                                    type="text"
                                    @click="handleHideAddSubLevel(data)">取消</el-button>
                            </div>
                        </div>
                    </div>
                </el-tree>
                <div class="btn-wrapper">
                    <el-button
                        size="small"
                        @click="handleShowAddTopLevelGroup">
                        <el-icon name="plus" />
                        添加一级分组
                    </el-button>
                    <div
                        class="add-panel large"
                        v-show="isAddingTopLevelGroup">
                        <el-input
                            ref="inputAddTopLevelGroup"
                            v-model="newTopLevelGroupName"
                            placeholder="输入组名"
                            @keyup.enter.native="handleResolveAddTopLevelGroup"/>
                        <div>
                            <el-button
                                type="primary"
                                size="mini"
                                @click="handleResolveAddTopLevelGroup">创建</el-button>
                            <el-button
                                type="text"
                                @click="handleHideAddTopLevelGroup">取消</el-button>
                        </div>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { generateRandomStr } from '@/assets/js/utils';
import { transform, transformRev } from "@/assets/js/transform";

export default {
    data() {
        return {
            authResult: {},
            groupList: [],
            isAddingTopLevelGroup: false, // 是否正在添加一级分组，控制输入模块显示
            newTopLevelGroupName: '', // 新增一级分组组名
        };
    },
    created() {
        this.$store.commit('SET_HEADER', [
            {
                title: '设置',
            },
            {
                title: '权限分组配置'
            }
        ]);
        this.authResult = this.$route.meta.authResult;
        this.getGroupList();
    },
    methods: {
        /**
         * 获取分组列表
         */
        getGroupList() {
            this.$post('/__local/system%5Cauth_group%5Cget_list', {}, resp => {
                if (resp.code == 1) {
                    if (Array.isArray(resp.data.list)) {
                        this.groupList = transform(resp.data.list);
                    } else {
                        this.groupList = [];
                    }
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg || '获取权限分组信息失败，请刷新重试'
                    });
                }
            });
        },
        /**
         * 显示添加一级分类输入框
         */
        handleShowAddTopLevelGroup() {
            if (this.isAddingTopLevelGroup) {
                return;
            }

            this.newTopLevelGroupName = '';
            this.isAddingTopLevelGroup = true;
            this.$nextTick(() => {
                this.$refs.inputAddTopLevelGroup.focus();
            });
        },

        /**
         * 隐藏添加一级分类输入框
         */
        handleHideAddTopLevelGroup() {
            this.isAddingTopLevelGroup = false;
        },

        /**
         * 添加一级分组
         */
        handleResolveAddTopLevelGroup() {
            if (!this.newTopLevelGroupName) {
                this.$message({
                    type: 'warning',
                    message: '请输入组名'
                });
                return;
            }

            let obj = {
                group_no: '',
                parent_group_no: '',
                new_id: generateRandomStr(),
                new_parent_id: '',
                title: this.newTopLevelGroupName
            };

            this.groupList.push(obj);

            this.handleHideAddTopLevelGroup();
        },

        /**
         * 显示添加子组输入框
         */
        handleShowAddSubLevel(data) {
            this.$set(data, 'tempTitle', '');
            this.$set(data, 'isAddingSubLevelGroup', true);
            this.$nextTick(() => {
                this.$refs[`new_sub_${data.group_no || data.new_id}`].focus();
            });
        },

        /**
         * 隐藏添加子组输入框
         */
        handleHideAddSubLevel(data) {
            this.$set(data, 'isAddingSubLevelGroup', false);
        },

        /**
         * 添加子组
         */
        handleResolveAddSubLevelGroup(data) {
            if (!data.tempTitle) {
                this.$message({
                    type: 'warning',
                    message: '请输入组名'
                });
                return;
            }

            let obj = {
                group_no: '',
                parent_group_no: data.group_no,
                new_id: generateRandomStr(),
                new_parent_id: '',
                title: data.tempTitle
            };

            if (data.new_id) {
                obj.parent_group_no = '';
                obj.new_parent_id = data.new_id;
            }

            if (!data.children) {
                this.$set(data, 'children', []);
            }
            data.children.push(obj);

            this.handleHideAddSubLevel(data);
        },

        /**
         * 删除分组
         */
        handleDeletSubLevel(node,data) {
            const parent = node.parent;
            const children = parent.data.children || parent.data;
            const index = children.findIndex(d => d.group_no === data.group_no);

            children.splice(index, 1);
        },

        /**
         * 显示重命名输入框
         */
        handleShowEditTitle(data) {
            this.$set(data, 'tempTopTitle', data.title);
            this.$set(data, 'isEditingTitle', true);
            this.$nextTick(() => {
                this.$refs[`edit_${data.group_no || data.new_id}`].focus();
            });
        },

        /**
         * 隐藏重命名输入框
         */
        handleHideEditTitle(data) {
            this.$set(data, 'isEditingTitle', false);
        },

        /**
         * 重命名分组名称
         */
        handleResolveEditTitle(data) {
            data.title = data.tempTopTitle;
            data.isEditingTitle = false;
        },

        /**
         * 提交分组变更信息
         */
        submitGroup() {
            this.$post('/__local/system%5Cauth_group%5Cset_groups', {
                data: JSON.stringify(transformRev(this.groupList))
            }, resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: '设置成功'
                    });
                    this.getGroupList();
                }
            });
        },
    }
};
</script>

<style lang="less" scoped>
.container {
    display: flex;
    flex-direction: column;
}
.operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;
}
.tree-row {
    flex: 1;
    margin: 15px;
    background-color: #fff;
    overflow: auto;
}
.tree-col {
    padding: 20px;
}
.custom-tree-node {
    flex: 1;

    &__content {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        padding-right: 8px;
    }

    .add-tree-node {
        padding-left: 16px;
    }

    .el-button--text.danger {
        color: #f56c6c;
    }
}
.el-tree {
    color: #000;
}
.btn-wrapper {
    margin-top: 20px;
}
.add-panel {
    height: 114px;
    margin-top: 10px;
    padding: 0 15px;
    background-color: #fff;
    border: 1px solid #e8e8e8;
    border-radius: 2px;

    &.large {
        height: 150px;
    }

    &.inside {
        margin: 10px;
    }

    .el-input {
        margin-bottom: 15px;
    }
}
</style>

<style lang="less">
.auth-group-container {
    .el-tree {
        .el-tree-node__expand-icon {
            align-self: flex-start;
        }
    }
    .el-input__inner {
        padding: 0;
        border: none;
        border-bottom: 1px solid #e9e9e9;
        background-color: transparent;
    }
    .el-tree-node__content {
        height: auto!important;
    }
    .el-tree__empty-text {
        left: 0;
        transform: translate(0, -50%);
    }
}
</style>

